﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DatabaseCompare.Model;
using DatabaseCompare.Service.Compare;

namespace DatabaseCompare.ServiceImpl.Compare
{


    /// <summary>
    /// 数据库比较.
    /// </summary>
    public class DatabaseCompare :
        CompareAbleCompare<DatabaseInfo>, 
        ICompareAbleCompare<DatabaseInfo, DatabaseCompareResult>
    {

        /// <summary>
        /// 表的比较类.
        /// </summary>
        private ICompareAbleListCompare<TableInfo, TableCompareResult> tableListCompare = new TableListCompare();


        /// <summary>
        /// 视图的比较类.
        /// </summary>
        private ICompareAbleListCompare<ViewInfo, ViewCompareResult> viewListCompare = new ViewListCompare();

        /// <summary>
        /// 触发器 的比较类.
        /// </summary>
        private ICompareAbleListCompare<TriggerInfo, TriggerCompareResult> triggerListCompare = new TriggerListCompare();

        /// <summary>
        /// 存储过程 的比较类.
        /// </summary>
        private ICompareAbleListCompare<ProcedureInfo, ProcedureCompareResult> procedureListCompare = new ProcedureListCompare();


        /// <summary>
        /// 函数 的比较类.
        /// </summary>
        private ICompareAbleListCompare<FunctionInfo, FunctionCompareResult> functionListCompare = new FunctionListCompare();




        /// <summary>
        /// 两个数据库比较.
        /// </summary>
        /// <param name="leftDatabase"></param>
        /// <param name="rightDatabase"></param>
        /// <returns></returns>
        public DatabaseCompareResult Compare(DatabaseInfo leftDatabase, DatabaseInfo rightDatabase)
        {
            // 比较结果.
            DatabaseCompareResult result = new DatabaseCompareResult();

            // 设置比较参数.
            result.Left = leftDatabase;
            result.Right = rightDatabase;


            // 首先简单作非空的判断.
            result.CompareResult = base.CommonCompare(leftDatabase, rightDatabase);

            if (result.CompareResult == CommonCompareResult.FullSame)
            {
                // 左右都不为空
                // 需要详细比较.


                // 开始比较数据库下的表信息.

                // 表 比较结果列表.
                result.TableCompareResultList =
                    tableListCompare.Compare(leftDatabase.TableList, rightDatabase.TableList);

                // 比较表完毕后，只要表 里面，有一个不完全匹配
                // 那么认为 数据库 不匹配.
                if (result.TableCompareResultList != null
                    && result.TableCompareResultList.Count > 0)
                {
                    if (result.TableCompareResultList.Count(p => p.CompareResult != CommonCompareResult.FullSame) > 0)
                    {
                        result.CompareResult = CommonCompareResult.Difference;
                    }
                }


                // 视图 比较结果列表
                result.ViewCompareResultList =
                    viewListCompare.Compare(leftDatabase.ViewList, rightDatabase.ViewList);

                // 比较视图完毕后，只要视图 里面，有一个不完全匹配
                // 那么认为 数据库 不匹配.
                if (result.ViewCompareResultList != null
                    && result.ViewCompareResultList.Count > 0)
                {
                    if (result.ViewCompareResultList.Count(p => p.CompareResult != CommonCompareResult.FullSame) > 0)
                    {
                        result.CompareResult = CommonCompareResult.Difference;
                    }
                }


                // 触发器 比较结果列表
                result.TriggerCompareResultList =
                    triggerListCompare.Compare(leftDatabase.TriggerList, rightDatabase.TriggerList);

                // 比较触发器完毕后，只要触发器 里面，有一个不完全匹配
                // 那么认为 数据库 不匹配.
                if (result.TriggerCompareResultList != null
                    && result.TriggerCompareResultList.Count > 0)
                {
                    if (result.TriggerCompareResultList.Count(p => p.CompareResult != CommonCompareResult.FullSame) > 0)
                    {
                        result.CompareResult = CommonCompareResult.Difference;
                    }
                }


                // 存储过程.
                result.ProcedureCompareResultList =
                    procedureListCompare.Compare(leftDatabase.ProcedureList, rightDatabase.ProcedureList);
                // 比较存储过程完毕后，只要存储过程 里面，有一个不完全匹配
                // 那么认为 数据库 不匹配.
                if (result.ProcedureCompareResultList != null
                    && result.ProcedureCompareResultList.Count > 0)
                {
                    if (result.ProcedureCompareResultList.Count(p => p.CompareResult != CommonCompareResult.FullSame) > 0)
                    {
                        result.CompareResult = CommonCompareResult.Difference;
                    }
                }


                // 函数.
                result.FunctionCompareResultList =
                    functionListCompare.Compare(leftDatabase.FunctionList, rightDatabase.FunctionList);
                // 比较函数完毕后，只要函数 里面，有一个不完全匹配
                // 那么认为 数据库 不匹配.
                if (result.FunctionCompareResultList != null
                    && result.FunctionCompareResultList.Count > 0)
                {
                    if (result.FunctionCompareResultList.Count(p => p.CompareResult != CommonCompareResult.FullSame) > 0)
                    {
                        result.CompareResult = CommonCompareResult.Difference;
                    }
                }

            }

            // 返回.
            return result;
        }



    }


}
